/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.view;

import br.jm.financeiro.base.eao.EaoException;
import br.jm.financeiro.datamodel.LazyPropostaEtapa;
import br.jm.financeiro.entity.PropostaEtapa;
import br.jm.financeiro.entity.PropostaEtapaDesc;
import br.jm.financeiro.entity.PropostaEtapaPerc;
import br.jm.financeiro.sistema.eao.PropostaEtapaDescEao;
import br.jm.financeiro.sistema.eao.PropostaEtapaEao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import org.primefaces.context.RequestContext;
import org.primefaces.event.ToggleEvent;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author joao
 */
public class PropostaEtapaBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private PropostaEtapa selectedPropostaEtapa;
    private PropostaEtapaDesc selectedPropostaEtapaDesc;
    private PropostaEtapaPerc selectedPropostaEtapaPerc;
    private LazyDataModel<PropostaEtapa> lazyModel;
    private final PropostaEtapaEao propostaEtapaEAO;
    private final PropostaEtapaDescEao propostaEtapaDescEAO;
    private FacesMessage msg = null;
    private boolean altera = true;
    private boolean alteraPerc = true;
    private boolean alteraDesc = true;
    private int rowid;
    private int rowidesc;
    public EntityManager sessao;
    private String cFiltro;

    public PropostaEtapaBean() {
        propostaEtapaEAO = PropostaEtapaEao.getInstance();
        propostaEtapaDescEAO = PropostaEtapaDescEao.getInstance();

        selectedPropostaEtapa = new PropostaEtapa();
        selectedPropostaEtapaDesc = new PropostaEtapaDesc();
        selectedPropostaEtapaPerc = new PropostaEtapaPerc();

        lazyModel = new LazyPropostaEtapa(propostaEtapaEAO);
    }

    public PropostaEtapa getSelectedPropostaEtapa() {
        return selectedPropostaEtapa;
    }

    public void setSelectedPropostaEtapa(PropostaEtapa selectedPropostaEtapa) throws EaoException {
        /*
        

         this.selectedPropostaEtapaDesc.setIdEtapa(selectedPropostaEtapa);
         this.selectedPropostaEtapaPerc.setIdEtapaDesc(selectedPropostaEtapaDesc);

         cFiltro = " id_etapa = " + selectedPropostaEtapa.getIdEtapa();

         selectedPropostaEtapa.setPropostaEtapaDescList(propostaEtapaDescEAO.getNative("PropostaEtapaDesc", 0, propostaEtapaDescEAO.getCount("PropostaEtapaDesc"), null, true, null, null, cFiltro));
         */
        List<PropostaEtapaDesc> propostaEtapaDescList = selectedPropostaEtapa.getPropostaEtapaDescList();
        Collections.sort(propostaEtapaDescList);
        selectedPropostaEtapa.setPropostaEtapaDescList(propostaEtapaDescList);
        this.selectedPropostaEtapaDesc = new PropostaEtapaDesc();
        this.selectedPropostaEtapaPerc = new PropostaEtapaPerc();

        this.selectedPropostaEtapa = selectedPropostaEtapa;
    }

    public LazyDataModel<PropostaEtapa> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<PropostaEtapa> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public PropostaEtapaPerc getSelectedPropostaEtapaPerc() {
        return selectedPropostaEtapaPerc;
    }

    public void setSelectedPropostaEtapaPerc(PropostaEtapaPerc selectedPropostaEtapaPerc) {
        this.selectedPropostaEtapaPerc = selectedPropostaEtapaPerc;
    }

    public PropostaEtapaDesc getSelectedPropostaEtapaDesc() {
        return selectedPropostaEtapaDesc;
    }

    public void setSelectedPropostaEtapaDesc(PropostaEtapaDesc selectedPropostaEtapaDesc) {
        this.selectedPropostaEtapaDesc = selectedPropostaEtapaDesc;
    }

    public boolean isAlteraDesc() {
        return alteraDesc;
    }

    public void setAlteraDesc(boolean alteraDesc) {
        this.alteraDesc = alteraDesc;
    }

    public void inserir(ActionEvent actionEvent) throws EaoException {
        try {
            selectedPropostaEtapa.setIdEtapa(null);

            if (selectedPropostaEtapa.getNomEtapa().trim().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Nome da Proposta não pode ficar em Branco!", "");
            } else if (selectedPropostaEtapa.getPropostaEtapaDescList().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Criar pelo menos 1 (Hum) Escopo!", "");
            } else {
                propostaEtapaEAO.create(selectedPropostaEtapa);

                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Etapas - Honorários Inserido com Sucesso: ".concat(selectedPropostaEtapa.getNomEtapa())), selectedPropostaEtapa.getNomEtapa());
            }

        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Inserir: " + selectedPropostaEtapa.getNomEtapa(), e.getMessage());
        } finally {
            selectedPropostaEtapa = new PropostaEtapa();
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void alterar(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();

        try {
            if (selectedPropostaEtapa.getNomEtapa().trim().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Nome da Proposta não pode ficar em Branco!", "");
            } else if (selectedPropostaEtapa.getPropostaEtapaDescList().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Criar pelo menos 1 (Hum) Escopo!", "");
            } else {
                propostaEtapaEAO.edit(selectedPropostaEtapa);

                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Etapas - Honorários Alterado com Sucesso: ".concat(selectedPropostaEtapa.getNomEtapa())), selectedPropostaEtapa.getNomEtapa());
            }

        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Alterar: " + selectedPropostaEtapa.getNomEtapa(), e.getMessage());
        } finally {
            selectedPropostaEtapa = new PropostaEtapa();
            FacesContext.getCurrentInstance().addMessage(null, msg);
            context.update("frmPropostaEtapa:panel");
        }
    }

    public void excluir(ActionEvent actionEvent) {
        try {
            propostaEtapaEAO.destroy(selectedPropostaEtapa);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Etapas - Honorários Excluido com Sucesso: ".concat(selectedPropostaEtapa.getNomEtapa())), selectedPropostaEtapa.getNomEtapa());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Excluir: " + selectedPropostaEtapa.getNomEtapa(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void preparaInserir(ActionEvent actionEvent) {
        this.altera = true;
        this.alteraDesc = true;
        this.alteraPerc = true;

        this.selectedPropostaEtapa = new PropostaEtapa();
        this.selectedPropostaEtapa.setPropostaEtapaDescList(new ArrayList<PropostaEtapaDesc>());

        this.selectedPropostaEtapaDesc = new PropostaEtapaDesc();
        this.selectedPropostaEtapaDesc.setPropostaEtapaPercList(new ArrayList<PropostaEtapaPerc>());
        this.selectedPropostaEtapaDesc.setIdEtapa(selectedPropostaEtapa);

        this.selectedPropostaEtapaPerc = new PropostaEtapaPerc();

    }

    public void preparaInserirDesc(ActionEvent actionEvent) {
        this.alteraDesc = true;
        this.alteraPerc = true;
        this.selectedPropostaEtapaDesc = new PropostaEtapaDesc();
        this.selectedPropostaEtapaDesc.setPropostaEtapaPercList(new ArrayList<PropostaEtapaPerc>());
        this.selectedPropostaEtapaPerc = new PropostaEtapaPerc();

        this.selectedPropostaEtapaDesc.setNumEtapa(Integer.toString(this.selectedPropostaEtapa.getPropostaEtapaDescList().size() + 1));
    }

    public void preparaInserirPerc(ActionEvent actionEvent) throws EaoException {
        msg = null;
        this.alteraPerc = true;
        this.selectedPropostaEtapaPerc = new PropostaEtapaPerc();
        this.selectedPropostaEtapaPerc.setIdEtapaDesc(selectedPropostaEtapaDesc);

        try {
            if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList() == null) {
                this.selectedPropostaEtapaDesc.setPropostaEtapaPercList(new ArrayList<PropostaEtapaPerc>());
            }
            int nNumEtapa = this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() + 1;

            this.selectedPropostaEtapaPerc.setDscPerc("R$ " + "#VALOR_ETAPA" + nNumEtapa + "#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat("#"));
            int nNumOrdem = Integer.parseInt(this.selectedPropostaEtapaDesc.getNumEtapa()) + nNumEtapa;
            this.selectedPropostaEtapaPerc.setNumOrdem(nNumOrdem);

            /*
            if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().isEmpty()) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA1#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "1");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 1) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA2#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "2");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 2) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA3#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "3");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 3) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA4#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "4");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "5");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "6");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "7");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "8");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "9");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "10");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "11");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "12");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "13");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "14");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "15");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "16");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "17");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "18");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "19");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "20");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "21");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "22");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "23");
            } else if (this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size() == 4) {
                this.selectedPropostaEtapaPerc.setDscPerc("R$ #VALOR_ETAPA5#" + this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "#");
                this.selectedPropostaEtapaPerc.setNumOrdem(this.selectedPropostaEtapaDesc.getNumEtapa().trim().concat(this.selectedPropostaEtapaDesc.getNumEtapaCompl().trim()) + "24");
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Etapas - Honorários não pode ter mais que 5 Percentuais!", selectedPropostaEtapa.getNomEtapa());
            }
                    */
        } finally {
            if (msg != null) {
                FacesContext.getCurrentInstance().addMessage(null, msg);
            }
        }
    }

    public boolean isAltera() {
        return altera;
    }

    public void setAltera(boolean altera) {
        this.altera = altera;
    }

    public boolean isAlteraPerc() {
        return alteraPerc;
    }

    public void setAlteraPerc(boolean alteraPerc) {
        this.alteraPerc = alteraPerc;
    }

    public void inserirDesc(ActionEvent actionEvent) {
        try {
            if (this.selectedPropostaEtapaDesc.getNumEtapa().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Numero do Escopo em Branco! ", selectedPropostaEtapa.getNomEtapa());
            } else {
                if (selectedPropostaEtapaDesc.getPropostaEtapaPercList().isEmpty()) {
                    msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Inserir Honorários! ", selectedPropostaEtapa.getNomEtapa());
                } else {
                    List<PropostaEtapaDesc> propostaEtapaDescList = this.selectedPropostaEtapa.getPropostaEtapaDescList();
                    propostaEtapaDescList.add(selectedPropostaEtapaDesc);

                    for (int i = 0; i < propostaEtapaDescList.size(); i++) {
                        this.selectedPropostaEtapaDesc = propostaEtapaDescList.get(i);
                        this.selectedPropostaEtapaDesc.setIdEtapa(selectedPropostaEtapa);

                        List<PropostaEtapaPerc> propostaEtapaPercList = new ArrayList<PropostaEtapaPerc>();

                        for (int j = 0; j < this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().size(); j++) {
                            this.selectedPropostaEtapaPerc = this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().get(j);
                            this.selectedPropostaEtapaPerc.setIdEtapaDesc(selectedPropostaEtapaDesc);

                            propostaEtapaPercList.add(selectedPropostaEtapaPerc);
                        }

                        Collections.sort(propostaEtapaPercList);

                        selectedPropostaEtapaDesc.setPropostaEtapaPercList(propostaEtapaPercList);
                        propostaEtapaDescList.set(i, selectedPropostaEtapaDesc);

                        this.selectedPropostaEtapa.setPropostaEtapaDescList(propostaEtapaDescList);

                        this.selectedPropostaEtapaDesc = new PropostaEtapaDesc();
                        this.selectedPropostaEtapaDesc.setIdEtapa(selectedPropostaEtapa);
                        this.selectedPropostaEtapaPerc = new PropostaEtapaPerc();

                        this.alteraDesc = true;
                        this.alteraPerc = true;

                    }
                    msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Escopo Inserido com Sucesso!", selectedPropostaEtapa.getNomEtapa());
                }
            }
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Inserir: " + selectedPropostaEtapa.getNomEtapa(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void alterarDesc(ActionEvent actionEvent) {
        try {
            if (selectedPropostaEtapaDesc.getNumEtapa().isEmpty()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Numero do Escopo em Branco! ", selectedPropostaEtapa.getNomEtapa());
            } else {
                if (selectedPropostaEtapaDesc.getPropostaEtapaPercList().isEmpty()) {
                    msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Inserir Honorários! ", selectedPropostaEtapa.getNomEtapa());
                } else {
                    this.selectedPropostaEtapa.getPropostaEtapaDescList().set(this.rowidesc, selectedPropostaEtapaDesc);
                    this.selectedPropostaEtapaDesc = new PropostaEtapaDesc();
                    this.alteraDesc = true;
                    this.alteraPerc = true;
                    msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Escopo Alterado com Sucesso!", selectedPropostaEtapa.getNomEtapa());
                }
            }
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Alterar: " + selectedPropostaEtapa.getNomEtapa(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void excluirDesc(ActionEvent actionEvent) {

        this.selectedPropostaEtapa.getPropostaEtapaDescList().remove(this.rowidesc);
        this.selectedPropostaEtapaDesc = new PropostaEtapaDesc();
        this.alteraDesc = true;
        this.alteraPerc = true;
    }

    public void inserirPerc(ActionEvent actionEvent) {
        try {
            if (selectedPropostaEtapaPerc.getPercEtapa().intValue() == 0) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Inserir o Percentual! ", selectedPropostaEtapa.getNomEtapa());
            } else {
                this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().add(selectedPropostaEtapaPerc);
                Collections.sort(this.selectedPropostaEtapaDesc.getPropostaEtapaPercList());
                this.selectedPropostaEtapaPerc = new PropostaEtapaPerc();
                this.alteraPerc = true;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Honorario Inserido com Sucesso!", selectedPropostaEtapa.getNomEtapa());
            }
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Alterar: " + selectedPropostaEtapa.getNomEtapa(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void alterarPerc(ActionEvent actionEvent) {
        try {
            if (selectedPropostaEtapaPerc.getPercEtapa().intValue() == 0) {
                msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ATENÇÃO ... Obrigatório Inserir o Percentual! ", selectedPropostaEtapa.getNomEtapa());
            } else {
                this.selectedPropostaEtapaDesc.getPropostaEtapaPercList().set(this.rowid, selectedPropostaEtapaPerc);
                Collections.sort(this.selectedPropostaEtapaDesc.getPropostaEtapaPercList());
                this.selectedPropostaEtapaPerc = new PropostaEtapaPerc();
                this.alteraPerc = true;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Honorario Alterado com Sucesso!", selectedPropostaEtapa.getNomEtapa());
            }
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao Alterar: " + selectedPropostaEtapa.getNomEtapa(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void excluirPerc(ActionEvent actionEvent) {
        this.selectedPropostaEtapaDesc.setPropostaEtapaPercList(new ArrayList<PropostaEtapaPerc>());
        this.selectedPropostaEtapaPerc = new PropostaEtapaPerc();
        this.alteraPerc = true;
    }

    public int getRowid() {
        return rowid;
    }

    public void setRowid(int rowid) {
        this.rowid = rowid;
    }

    public int getRowidesc() {
        return rowidesc;
    }

    public void setRowidesc(int rowidesc) {
        this.rowidesc = rowidesc;
    }

    public void onRowToggle(ToggleEvent event) throws EaoException {
        this.selectedPropostaEtapa = (PropostaEtapa) event.getData();
        cFiltro = " id_etapa = " + selectedPropostaEtapa.getIdEtapa();

        selectedPropostaEtapa.setPropostaEtapaDescList(propostaEtapaDescEAO.getNative("PropostaEtapaDesc", 0, propostaEtapaDescEAO.getCount("PropostaEtapaDesc"), null, true, null, null, cFiltro));
    }
}
